﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FinWeb3.Models.Repository
{
    public class BillRepository : IRepository<Bill>
    {
        public void Save(Bill item, System.Security.Principal.IIdentity user)
        {
            if (item.Id == 0)
            {
                item.UserName = user.Name;
                item.Save();
            }
            else
            {
                var bill = Get(item.Id, user);

                if (bill != null)
                {
                    bill.BankId = item.BankId;
                    bill.Category = item.Category;
                    bill.Day = item.Day;
                    bill.Description = item.Description;
                    bill.BaseValue = item.BaseValue;
                    bill.HasDetailList = item.HasDetailList;
                    bill.DeletedBill = item.DeletedBill;
                    bill.UserName = user.Name;
                    bill.GroupId = item.GroupId;
                    bill.IsPaid = item.IsPaid;
                    bill.StatementId = item.StatementId;
                    bill.Comments = item.Comments;
                    bill.ReceiveBill = item.ReceiveBill;
                    bill.ParentBillId = item.ParentBillId;
                    bill.Wallet = item.Wallet;

                    bill.Save();
                }
            }
        }

        public void Delete(Bill item, System.Security.Principal.IIdentity user)
        {
            var bill = Get(item.Id, user);

            if (bill != null)
                bill.Delete();
        }

        public Bill Get(int id, System.Security.Principal.IIdentity user)
        {
            return Bill.All().Where(x => x.UserName == user.Name && x.Id == id).FirstOrDefault();
        }

        public IQueryable<Bill> GetAll(System.Security.Principal.IIdentity user)
        {
            return Bill.All().Where(x => x.UserName == user.Name);
        }
    }
}
